import Vue from "vue";

import store from './store'
import app from './app'


let router = null;
let instance = null;

function render(props = {}, sharedVueConstructor = true) {
  // 通过 qiankun 拿到 Vue 实例，防止响应式锻链
  const { container } = props;

  if (sharedVueConstructor) {
    const { store, Vue } = props

    // 使用主应用传递过来的 Vue
    instance = new Vue({
      store,
      render: (h) => h(app),
    })
  } else {
    instance = new Vue({
      store,
      render: (h) => h(app),
    })
  }

  instance.$mount(container ? container.querySelector('#micro-app1') : '#micro-app1');
}

// 独立运行时
if (!window.__POWERED_BY_QIANKUN__) {
  render({}, false);
}

export async function bootstrap() {
  console.log('[vue] vue app bootstraped');
}
export async function mount(props) {
  console.log('[vue] props from main framework', props);
  render(props);
}
export async function unmount() {
  instance.$destroy();
  instance.$el.innerHTML = '';
  instance = null;
  router = null;
}
